<?php require_once 'header.php';?>    
<!-- DataTables Responsive CSS -->
    <link href="sb2/vendor/datatables-responsive/dataTables.responsive.css" rel="stylesheet">

        <!-- Page Content -->
        <div id="page-wrapper">
            <div class="container-fluid">
                <div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                        	<?php echo XXF_BACK?>
                            图书入库
                        </div>
                        <!-- /.panel-heading -->
                        <div class="panel-body">
                        	编号:<input id="mynum">
                        	<br>
                        	ISBN:<input id="isbn"><span id="status"></span>
							<div id="showbook"></div>
                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <!-- /.panel -->
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->
             </div>
        </div>
        <!-- /#page-wrapper -->
  </div>
        <!-- DataTables JavaScript -->
<?php require_once 'bottom.php';?>
    <script>
    function book(){
    	var that=this;
    	this.isbnapi="https://api.douban.com/v2/book/isbn/";
    	this.isbn="9787543632608";
    	this.isbnobj={};
    	this.tochn={
    		image:"图片",
    		isbn13:"ISBN13",
    		isbn10:"ISBN10",
    		title:"书名",
    		origin_title:"原名",
    		author:"作者",
    		translator:"译者",
    		binding:"装订",
    		pages:"页数",
    		pubdate:"出版日期",
    		publisher:"出版社",
    		author_intro:"作者介绍",
    		summary:"概要",
    		catalog:"目录",
    		price:"价格"
    	};
    	this.success=function(data){
    		that.isbnobj=data;
    		that.exec();
    	}
    	this.exec=function(){
    		that.show();
    	}
    	this.run=function(isbn){
    		isbn=isbn||this.isbn;
    		$.ajax({
    			url: this.isbnapi+isbn,
    			type: "GET",
    			dataType: 'JSONP',
    			success: this.success,
    			error:function (XMLHttpRequest, textStatus, errorThrown){
					console.log(textStatus);
            	}
    		});
    	}
    	this.show=function(){
    		var ele=document.getElementById(this.eleid)||document.body;
    		ele.innerHTML="";
    		for(e in this.tochn){
    			div=document.createElement("div");
    			div.innerHTML="<font color=red>"+this.tochn[e]+"</font>"+" : "+(e=='image'?("<img src='"+this.isbnobj[e]+"'>"):this.isbnobj[e]);
    			ele.appendChild(div);
    		}
    	}
    }

    var b=new book();
    b.eleid="showbook"
    b.exec=function(){
    	b.show();
    	b.isbnobj['author']=b.isbnobj['author'].join(",");
    	b.isbnobj['translator']=b.isbnobj['translator'].join(",");
    	b.isbnobj['mynum']=$("#mynum").val();
    	b.isbnobj['status']="正常";
    	console.log(b.isbnobj);
    	var send=function(){
			$.post("bookm",{
				isbnobj:b.isbnobj,
				type:"adddb"
			},function(data){
				console.log(data);
				$("#status").html("已保存");
			}).error(function(){
				send();
			});
        };
        send();
    }
    $("#isbn").keydown(function(e){
    	if(e.keyCode==13){
    		if($("#mynum").val().length<2){
				$.xalert("编号为空，请填上。");
				this.value="";
				return;
        	}
        	$("#status").html("");
    		b.run(this.value);
    		this.value="";
    	}
    }); 
    </script>